﻿using Framework.Save;
using Framework.Utils.SingletonTemplate;
using UnityEngine;

public class Account : MgrBase<Account> 
{
    public GameData PlayerData;
    public SettingData SettingData;
    public string UserID { get; private set; }
    protected override void InitMge()
    {
        this.Load();
    }
    public void Load() 
    {
        if (PlayerPrefs.HasKey("USERID"))
        {
            UserID = PlayerPrefs.GetString("USERID");
        }
        else 
        {
            UserID = System.Guid.NewGuid().ToString();
            PlayerPrefs.SetString("USERID", UserID);
        }

        if (SaveManager.Instance.HasSave(SaveData.GameSystem) == false)
        {
            this.PlayerData = new GameData();
            this.PlayerData.NullCheck();
            SaveManager.Instance.Save(SaveData.GameSystem, this.PlayerData);
        }
        else 
        {
            this.PlayerData = SaveManager.Instance.Load<GameData>(SaveData.GameSystem);
            this.PlayerData.NullCheck();
        }

        if (SaveManager.Instance.HasSave(SaveData.GameSetting) == false)
        {
            this.SettingData = new SettingData();
            SaveManager.Instance.Save(SaveData.GameSetting, this.SettingData);
        }
        else
        {
            this.SettingData = SaveManager.Instance.Load<SettingData>(SaveData.GameSetting);
            if (SettingData == null) 
            {
                SettingData = new SettingData();
                SaveManager.Instance.Save(SaveData.GameSetting, this.SettingData);
            }
        }
    }
    public void Save() 
    {
        SaveManager.Instance.Save(SaveData.GameSystem, this.PlayerData);
        SaveManager.Instance.Save(SaveData.GameSetting, this.SettingData);
    }

    public void Save(SaveData saveData) 
    {
        switch (saveData)
        {
            case SaveData.GameSystem:
                SaveManager.Instance.Save(SaveData.GameSystem, this.PlayerData);
                break;
            case SaveData.GameSetting:
                SaveManager.Instance.Save(SaveData.GameSetting, this.SettingData);
                break;
            default:
                break;
        }
    }


}